<?php
class ControllerSalePaypalExpress extends AdminController {
	
	public function index() {
		$this->document->setTitle('Paypal Express Sessions');

		$qf = new Query_filter(null, false);
		$page = $qf->get('page', 1);
		$order_id = $qf->get('order_id', '');
		
		$this->data['order_id'] = $order_id;
		
		$sql = "SELECT id,order_id,token,method,ip,date_added FROM @@paypal_express_session";
		$where = "";
		if ($order_id) {
			$where = " WHERE order_id=$order_id";
		}
		$sql .= "$where ORDER BY id DESC";
		
		if ($page < 0) $page = 1;
		$offset = ($page - 1) * C('config_admin_limit');
		$sql .= " LIMIT $offset, " . C('config_admin_limit');
		
		$total = $this->db->queryOne("SELECT count(*) as total FROM @@paypal_express_session $where");
		$query = $this->db->query($sql);
		$this->data['sessions'] = $query->rows;
		$url = UA('sale/paypal_express', 'page={page}');
		$this->data['pagination'] = $this->pagination($total, $url, null, $page)->render();
		
		$this->display('payment/paypal_express_session.tpl');
	}
	
	public function session_info() {
		$id = $this->request->get('id');
		$rs = $this->db->queryOne("SELECT * FROM @@paypal_express_session WHERE id = $id");
		if ($rs) {
			$output = "<table border=0 bgcolor=#88A8AC cellspacing='1' cellpadding='5'>";
			$output .= "<tr bgcolor='#ffffff'><th>Order ID</th><td>{$rs['order_id']}</td><th>Token</th><td>{$rs['token']}</td><th>Method</th><td>{$rs['method']}</td></tr>";
			$output .= "<tr bgcolor='#ffffff'><th>Request</th><td colspan='5'><div style='height:150px;overflow:scroll'>".str_replace('&', "<br>", $rs['request'])."</div></td></tr>";
			$output .= "<tr bgcolor='#ffffff'><th>Response</th><td colspan='5'><div style='height:150px;overflow:scroll'>".str_replace('&', "<br>", $rs['response'])."</div></td></tr>";
			$output .= "</table>";
		}
		else {
			$output = "No record";
		}
		$this->response->setOutput($output);
	}
	
	public function payment() {
		$this->document->setTitle('Paypal Express Payments');
		
		$qf = new Query_filter(null, false);
		$page = $qf->get('page', 1);
		$order_id = $qf->get('order_id', '');
		$transaction_id = $qf->get('transaction_id', '');
		
		$this->data['order_id'] = $order_id;
		$this->data['transaction_id'] = $transaction_id;
		
		$sql = "SELECT * FROM @@paypal_express_payment";
		$where = " WHERE 1";
		if ($order_id) {
			$where .= " AND order_id=$order_id";
		}
		if ($transaction_id) {
			$where .= " AND transaction_id='$transaction_id'";
		}
		$sql .= "$where ORDER BY id DESC";
		if ($page < 0) $page = 1;
		$offset = ($page - 1) * C('config_admin_limit');
		$sql .= " LIMIT $offset, " . C('config_admin_limit');
		
		$total = $this->db->queryOne("SELECT count(*) as total FROM @@paypal_express_payment $where");
		$query = $this->db->query($sql);
		$this->data['payments'] = $query->rows;
		
		$url = UA('sale/paypal_express/payment', 'page={page}');
		$this->data['pagination'] = $this->pagination($total, $url, null, $page)->render();
		
		$this->display('payment/paypal_express_payment.tpl');
	}
	
	public function payment_info() {
		$id = $this->request->get('id');
		$peInfo = $this->db->queryOne("SELECT * FROM @@paypal_express_payment WHERE id = $id");
		if ($peInfo) {
			$output = "<table border=0 bgcolor='#88A8AC' cellspacing='1' cellpadding='5'><tr bgcolor='#ffffff'>";
			$i = 0;
			foreach($peInfo as $k => $v) {
				$i++;
				$output .= "<th>$k</th><td>$v</td>";
				if ($i % 2 == 0) {
					$output .= "</tr><tr bgcolor='#ffffff'>";
				}				
			}
			$output .= "</tr></table>";
		}
		else {
			$output = "No record";
		}
		$this->response->setOutput($output);
	}
	
	public function error() {
		$this->document->setTitle('Paypal Express Errors');
		
		$qf = new Query_filter(null, false);
		$page = $qf->get('page', 1);
		$order_id = $qf->get('order_id', '');
		
		$this->data['order_id'] = $order_id;
		
		$sql = "SELECT * FROM @@paypal_express_error";
		$where = '';
		if ($order_id) {
			$where = " WHERE order_id=$order_id";
		}
		$sql .= "$where ORDER BY id DESC";
		
		if ($page < 0) $page = 1;
		$offset = ($page - 1) * C('config_admin_limit');
		$sql .= " LIMIT $offset, " . C('config_admin_limit');
		
		$total = $this->db->queryOne("SELECT count(*) as total FROM @@paypal_express_error $where");
		$query = $this->db->query($sql);
		$this->data['errors'] = $query->rows;
		
		$url = UA('sale/paypal_express/error', 'page={page}');
		$this->data['pagination'] = $this->pagination($total, $url, null, $page)->render();
		
		$this->display('payment/paypal_express_error.tpl');
	}
	
	public function delete() {
		$type = G('type');
		$action = G('action', 'delete');
		
		if ($action == 'delete' && empty($this->request->post['selected'])) {
			$this->redirect(UA('sale/paypal_express'));
		}
		
		if ($action == 'delete') {
			$where  = 'id IN (' . implode(',', $this->request->post['selected']) . ')';
		}
		else {
			$where = "DATE_SUB(CURDATE(),INTERVAL 20 DAY) > date_added";
		}
		if ($type == 'error') {
			$this->db->runSql("DELETE FROM @@paypal_express_error WHERE $where");
			$this->redirect(UA('sale/paypal_express/error', 'filter_reset=1'));
		}
		elseif ($type == 'session') {
			$this->db->runSql("DELETE FROM @@paypal_express_session WHERE $where");
			$this->redirect(UA('sale/paypal_express', 'filter_reset=1'));
		}
		elseif ($type == 'payment') {
			$this->db->runSql("DELETE FROM @@paypal_express_payment WHERE $where");
			$this->redirect(UA('sale/paypal_express/payment', 'filter_reset=1'));
		}
	}
}